package com.bocloud.http;

import com.alibaba.fastjson.JSONObject;
import com.bocloud.model.HttpResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * Created by Administrator on 2019/1/22.
 */
@Component
public class HttpUtil {

    Logger log = LoggerFactory.getLogger(HttpUtil.class);

    public HttpResult sentForm(String url, Map<String, String> headers, HttpMethod method, Object params, String encoding) throws UnsupportedEncodingException {
        HttpResult httpResult = new HttpResult();
        if (null == encoding) {
            encoding = "utf-8";
        }
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders httpHeaders = new HttpHeaders();
        //httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        if (null != headers && headers.size() > 0) {
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                httpHeaders.set(entry.getKey(), entry.getValue());
            }
        }
        if (null != params) {
            String paramsForm = String.valueOf(params);
            log.info(paramsForm);
            HttpEntity requestEntity = new HttpEntity(paramsForm, httpHeaders);
            ResponseEntity<HttpResult> response = restTemplate.exchange(url, method, requestEntity, HttpResult.class);
            Integer code = httpResult.setCode(response.getStatusCodeValue());
            log.info("code:" + code);
            log.info("form" + response.getBody());
            return response.getBody();
        }
        return null;
    }

    public HttpResult sentJson(String url, Map<String, String> headers, HttpMethod method, Object params, String encoding) throws UnsupportedEncodingException {
        HttpResult httpResult = new HttpResult();
        if (null == encoding) {
            encoding = "utf-8";
        }
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders httpHeaders = new HttpHeaders();
        //httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        if (null != headers && headers.size() > 0) {
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                httpHeaders.set(entry.getKey(), entry.getValue());
            }
        }

        if (null != params) {
            String paramsJson = JSONObject.toJSONString(params);
            log.info(paramsJson);
            HttpEntity requestEntity = new HttpEntity(paramsJson, httpHeaders);
            log.info("" + requestEntity);
            ResponseEntity<HttpResult> response = restTemplate.exchange(url, method, requestEntity, HttpResult.class);
            Integer code = httpResult.setCode(response.getStatusCodeValue());
            log.info("code:" + code);
            log.info("" + response.getBody());
            return response.getBody();
        }
        return null;
    }
}
